/*
 * Our custom test view component takes an initial
 * parameters that make up the test, and will be stored 
 * in an external database 
 * 
 */
function TestView(params) {
	//create and populate the test object
	var instance = Util.view({
		layout:'horizontal',
		height:60
	});
	
	var title = params.title || '';
	
	var defaultFontSize = 14;
								
	var controlMap = ['fn', 'fp', 'dp', 'dn']; // Controls mapping to location: 
	var controls = [];	// another instance variable
					// containing an array of 
					// ImageView objects representing
					// the controls
					
	var images =  Util.images();	// Retrieve and cache images
					
	var textValue = Util.label({
		text:title,
		height:48,
		width:'auto',
		//left:5,
		color:'BLACK',
		font: {
			fontSize:defaultFontSize
		}
	});
	var testSelector = Ti.UI.createView({
		left: 10,
		width: 200,
		backgroundImage: 'images/bg_clear.png',
		borderWidth: 2,
		borderRadius: 5,
		borderColor: 'BLACK'
	});
	testSelector.add(textValue);
	instance.add(testSelector);
	
	// Closure to access/update state of the test selector
	instance.addEventListener('controlSelected', function(e){
		manageTestSelector(e);
	});
	
	//Create the necessary view structures to represent the
	//current value of the test
	for (var i = 0; i < 4; i++) {
		// Reset all off
		var fg = Util.label({height:25, width:25, label:controlMap[i]});			

		var control = Util.image({
			height:50,
			width:50,
			left:15
		});
		control.add(fg);
		
		(function() {
			//we need to capture the value of "i"
			//for use in click handler functions
			//on our image views.  To do this, we
			//use a closure (this self-calling function)
			instance.selected = params.selected || ''; 	// instance variable indicating 
														// which control was pre-selected
			Ti.API.info('Selected button is ' + instance.selected);
			var state = { type: controlMap[i], parent: instance };

			control.addEventListener('click', function() {
				setControl(state);
			});
		})();
		
		controls.push(control);
		instance.add(control);
		
	}
	
	//inner function to update view to reflect the current
	//test
	function setControl(state) {
		var selector = getIndexForType(state.type);
		
		Ti.API.debug('++++ Selected:' + selector + " Prior: " + state.parent.selected);	

		for (var i = 0, l = controlMap.length; i < l; i++) {
			if (i === 0) {
				if (selector == i && state.parent.selected !== state.type) {
					controls[i].image = images['go'];
				} else {
					controls[i].image = images['go_off'];
				}
			}
			else if (i > 0 && i < 3) {
				if (selector == i && state.parent.selected !== state.type) {
					controls[i].image = images['yield'];
				} else {
					controls[i].image = images['yield_off'];
				}
			}
			else {
				if (selector == i && state.parent.selected !== state.type) {
					controls[i].image = images['stop'];
				} else {
					controls[i].image = images['stop_off'];
				}
			}
		}
		
		state.parent.selected = state.parent.selected === state.type ? '' : state.type;

		// Notification to manage other parts of the view				
		if (state.type) {
			instance.fireEvent('controlSelected',{
				currentValue: state.type,
				lastSelected: state.parent.selected
			});
		}
	}
	
	function getIndexForType(type) {
		for (var i=0, l=controlMap.length; i < l; i++) {
			if (type === controlMap[i])
				return i;
		}
	}
	
	function manageTestSelector(e, control) {
		var type = e.currentValue;
		var selected = e.lastSelected;
		Ti.API.debug('Type: ' + type + 
				' Selected: ' + selected + 
				' Label: ' + textValue.text + 
					' is {weight:' + textValue.font.fontWeight + ',size:'+textValue.font.fontSize+'}');
		testSelector.backgroundImage = 'images/bg_clear.png';
		testSelector.backgroundColor = 'GRAY';
		testSelector.borderColor = 'BLACK';
		textValue.font = { fontWeight: 'normal', fontSize: defaultFontSize};
		textValue.color = 'BLACK';

		if (!type || !selected) {
			return;
		}
		
		testSelector.backgroundImage = 'images/bg_gradient.png';
		
		var i = getIndexForType(type);	// ['fn', 'fp', 'dp', 'dn']
		// Manage state of test selector
		if (i == 0) {
			Ti.API.debug(type + ' should be "fn"');
			textValue.color = 'GRAY';
		}
		else if (i == 1) {
			Ti.API.debug(type + ' should be "fp"');
			textValue.font = { fontWeight: 'bold'};
			testSelector.borderColor = 'YELLOW';
		}
		else {
			Ti.API.debug(type + ' should be "dn"');
			textValue.color = 'RED';
			testSelector.borderColor = 'RED';
		}
	}
			
	//initialize view and return instance
	setControl({ type: '', parent: instance }); 
	return instance;
};

//Public Module API
exports.TestView = TestView;